package com.wnxy.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.portal.common.constant.RedisConstant;
import com.wnxy.portal.common.result.Result;
import com.wnxy.portal.entity.Address;
import com.wnxy.portal.entity.China;
import com.wnxy.portal.mapper.AddressMapper;
import com.wnxy.portal.service.IAddressService;
import com.wnxy.portal.service.IChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jet
 * @since 2023-12-11
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IChinaService chinaService;

    @Override
    public List<China> getAllProvince() {
        List<China> list = null;
        // 判断缓存中是否有key
        if (!redisTemplate.hasKey(RedisConstant.PROVINCES)) {
            // 查询数据库
            list = chinaService.list(
                    Wrappers.lambdaQuery(China.class)
                            .eq(China::getPid, 0)
                            .ne(China::getId, 0)
            );
            // 查询结果放入Redis缓存，通过set集合存储
            list.forEach(china -> {
                redisTemplate.opsForSet().add(RedisConstant.PROVINCES, JSONUtil.toJsonStr(china));
            });
        } else {
            // 从Redis中获取
            Set<String> members = redisTemplate.opsForSet().members(RedisConstant.PROVINCES);
            list = members.stream().map(chinaString -> {
                System.out.println("chinaString = " + chinaString);
                // 现在需要把json转换为javabean对象
                China china = JSONUtil.toBean(chinaString,China.class);
                return china;
            }).collect(Collectors.toList());
        }

        return list;
    }

    // #root.methodName 动态获取方法名称
    // #provinceId 动态获取方法参数
    @Cacheable(cacheNames = "cities_cache",key = "#root.methodName + #provinceId")
    @Override
    public List<China> getAllCities(Integer provinceId) {
        List<China> list =
                chinaService.list(Wrappers.lambdaQuery(China.class)
                        .eq(Objects.nonNull(provinceId),China::getPid, provinceId));
        return list;
    }

    @Cacheable(cacheNames = "areas_cache",key = "#root.methodName + #cityId")
    @Override
    public List<China> getAllAreas(Integer cityId) {
        List<China> list =
                chinaService.list(Wrappers.lambdaQuery(China.class)
                        .eq(Objects.nonNull(cityId),China::getPid, cityId));
        return list;
    }
}
